db.stores.insert(
   [
     { _id: 1, name: "Java Hut", description: "Coffee and cakes" },
     { _id: 2, name: "Burger Buns", description: "Gourmet hamburgers" },
     { _id: 3, name: "Coffee Shop", description: "Just coffee" },
     { _id: 4, name: "Clothes Clothes Clothes", description: "Discount clothing" },
     { _id: 5, name: "Java Shopping", description: "Indonesian goods" }
   ]
)

db.stores.createIndex( { name: "text", description: "text" } );


db.stores.find( { $text: { $search: "java coffee shop" } } );


db.stores.find( { $text: { $search: "\"coffee shop\"" } } );

db.stores.find( { $text: { $search: "java shop -coffee" } } );

db.stores.find(
   { $text: { $search: "java coffee shop" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } );


db.subject.aggregate([
    {$project:{id:{ $toString: '$_id' },title:1}},
    {
        $lookup:
        {
            from: "topic",
            localField: "id",
            foreignField: "subjectId",
            as: "topics"
        }
    },
    {$unwind:"$topics"},
    {
        $project:{

            title:1,
            topics:{
                id:{$toString: '$topics._id'} ,
                name:1,
                type:1,
                order:1
            }
        }
    },
    {
        $lookup:
        {
            from: "topicOption",
            localField: "topics.id",
            foreignField: "topicId",
            as: "topics.topicOptions"
        }
    },
    {
        $group:{
            _id:   "$_id",
            title:{$first:'$title'},
            topics:{$push:'$topics'}
        }
    }
    ])   ;

db.subject.aggregate([
    {$project:{id:{ $toString: '$_id' },title:1}},
    {
        $lookup:
        {
            from: "topic",
            localField: "id",
            foreignField: "subjectId",
            as: "topics"
        }
    },
    {
        $project:{
            title:1,
            topics:{
                _id:{$toString:'$this'},
                id :{$toString: '$self._id'}
                te:{
                    '$map': {
                    'input': '$topics',
                    'as': 'value',
                    'in': {
                        $toString: '$$value._id'
                    }
                }
                }
                name:1,
                type:1,
                order:1
            }
        }
    }
]) ;



db.subject.aggregate([

    {
        $lookup:
        {
            let: {
                id: { $toString: '$_id' }
            },
            from: "topic",
            pipeline: [

            {
                "$match": {
                    "$expr": {
                        "$eq": ["$subjectId", "$$id"]
                    }
                }
            },
        ],

            as: "topics"
        }
    }
    ]) 